

import 'package:flutter/material.dart';

class OverlayPage extends StatefulWidget {
  @override
  _OverlayPageState createState() => _OverlayPageState();
}

class _OverlayPageState extends State<OverlayPage> {
  OverlayEntry? _overlayEntry;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          _showOverlay(context);
        },
        child: Text('显示Overlay'),
      ),
    );
  }

  void _showOverlay(BuildContext context) {
    // 创建OverlayEntry
    _overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: 100,
        left: 50,
        child: Material(
          color: Colors.transparent,
          child: Container(
            color: Colors.black54,
            width: 200,
            height: 100,
            child: Center(
              child: Text(
                '这是一个Overlay',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
          ),
        ),
      ),
    );

    // 插入OverlayEntry到Overlay中
    Overlay.of(context).insert(_overlayEntry!);

    // 3秒后移除OverlayEntry
    Future.delayed(Duration(seconds: 3), () {
      _overlayEntry?.remove();
      _overlayEntry = null;
    });
  }
}